from abc import ABC, abstractmethod
from urllib import request 

#定义抽象工厂
class Product(ABC):
    
    @abstractmethod
    def geturl(self):
        pass
    @abstractmethod
    def callback(self):
        pass
    
import requests  
import json
#定义实现类
class QQ(Product):
    def geturl(self):
        url = "http://qq.com/?cid=123&uri=http://"
        return url
    
    def callback(self,code):
        res = requests.post('')
        data = json.loads(res.text)
        return data['uid']
    
    
#定义实现类
class Weibo(Product):
    def geturl(self):
        client_id="3674019597"
        redirect_uri="http://127.0.0.1:8000/user/weiboCallback/"
        url = "https://api.weibo.com/oauth2/authorize?client_id=%s&response_type=code&redirect_uri=%s"%(client_id,redirect_uri)
        return url
    
    def callback(self,code):
        data = {"client_id":'3674019597',"client_secret":"7ae99bc0d54ead29fc8d1b9fabd78587",
        "grant_type":"authorization_code",
        'redirect_uri':'http://127.0.0.1:8000/user/weiboCallback/','code':code}
        url = "https://api.weibo.com/oauth2/access_token"
        res = requests.post(url,data=data)
        print(res.text)
        resdata = json.loads(res.text)
    #     1.获取code，accress_token接口发起Post请求，uid和token-》
        uid = resdata['uid']
        return uid
 
#定义实现类
class DingDing(Product):
    def geturl(self):
        client_id="3674019597"
        redirect_uri="http://127.0.0.1:8000/user/weiboCallback/"
        url = "https://api.weibo.com/oauth2/authorize?client_id=%s&response_type=code&redirect_uri=%s"%(client_id,redirect_uri)
        return url
    
    def callback(self,code):
        data = {"client_id":'3674019597',"client_secret":"7ae99bc0d54ead29fc8d1b9fabd78587",
        "grant_type":"authorization_code",
        'redirect_uri':'http://127.0.0.1:8000/user/weiboCallback/','code':code}
        url = "https://api.weibo.com/oauth2/access_token"
        res = requests.post(url,data=data)
        print(res.text)
        resdata = json.loads(res.text)
    #     1.获取code，accress_token接口发起Post请求，uid和token-》
        uid = resdata['uid']
        return uid

#定义工厂类  
class Factory(object):
    def create_factory(self,type):
        if type == 'qq':
            return QQ()
        if type == 'weibo':
            return Weibo()
        if type == 'dingding':
            return DingDing()
        
        
#实例化工厂类并调用
factory = Factory()
myclass = factory.create_factory('dingding')
print(myclass.geturl())